package md5;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class App {
	public static String change2MD5(String srcString) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException nsae) {
			return "";
		}

		byte[] md5Bytes = md.digest(srcString.getBytes());
		int tmpInt = 0;
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			tmpInt = (int) (md5Bytes[i] & 0xff);
			if (tmpInt < 16) {
				sb.append("0");
			}
			sb.append(Integer.toHexString(tmpInt));
		}
		return sb.toString();
	}
	
	public static void main(String[] args) {
		for(String arg : args) {
			System.out.println(change2MD5(arg));
			System.out.println(MD5.MD5_32(arg));
		}
	}
}

class MD5 {
	private static String byte2Hex(byte b) {
		int value = (b & 0x7F) + (b < 0 ? 0x80 : 0);
		return (value < 0x10 ? "0" : "")
			+ Integer.toHexString(value).toLowerCase();
	}
	
	public static String MD5_32(String password) {
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return "AlgorithmError";
		}
		StringBuffer strbuf = new StringBuffer();
		md5.update(password.getBytes(), 0, password.length());
		byte[] digest = md5.digest();
		
		for (int i = 0; i < digest.length; i++) {   
            strbuf.append(byte2Hex(digest[i]));   
        }
		
		return strbuf.toString();
	}
	
	/*public static void main(String[] args) throws Exception{
		System.out.println(MD5_32("1234"));
		Properties props = new Properties();
		props.load(ClassLoader.getSystemResourceAsStream("constants.properties"));
		System.out.println(props.getProperty("htmDir"));
		System.out.println(ClassLoader.getSystemResource("constants.properties").getPath());
	}*/
}

